import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;import java.util.Iterator;
public class OnlineShoppingSystem {
    private static Map<String, User> users = new HashMap<>(); // 模拟用户数据存储
    private static Map<String, String> emailToUsername = new HashMap<>(); // 模拟邮箱地址到用户名的映射
    private static Scanner scanner = new Scanner(System.in);
    public static void custmer() {
        boolean running = true;
        while (running) {
            System.out.println("欢迎使用购物系统！");
            System.out.println("1. 注册");
            System.out.println("2. 登录");
            System.out.println("3. 退出");
            System.out.print("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗掉输入缓冲区的换行符
            switch (choice) {
                case 1:
                    register();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    running = false;
                    System.out.println("感谢使用，再见！");
                    break;
                default:
                    System.out.println("无效的选择，请重新选择");
                    break;
            }
        }
        scanner.close();
    }
    // 注册功能
    private static void register() {
        System.out.println("==== 注册 ====");
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        // 检查用户名是否已存在
        if (users.containsKey(username)) {
            System.out.println("用户名已被注册，请重新选择用户名。");
            return;
        }
        // 检查用户名长度
        if (username.length() < 5) {
            System.out.println("用户名长度不能少于5个字符。");
            return;
        }
        System.out.print("请输入密码：");
        String password = scanner.nextLine();
        // 检查密码是否符合要求
        if (!isValidPassword(password)) {
            System.out.println("密码长度必须大于8个字符，必须包含大小写字母、数字和标点符号。");
            return;
        }
        // 注册成功
        User newUser = new User(username, password);
        users.put(username, newUser);
        System.out.println("注册成功！");
    }
    // 登录功能
    private static void login() {
        System.out.println("==== 登录 ====");
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        if (!users.containsKey(username)) {
            System.out.println("用户名不存在。");
            return;
        }
        User user = users.get(username);
        // 检查账户是否被锁定
        if (user.isLocked()) {
            System.out.println("账户已被锁定，请联系客服或重置密码。");
            return;
        }
        int attempts = 0;
        while (attempts < 5) {
            System.out.print("请输入密码：");
            String password = scanner.nextLine();

            if (user.login(password)) {
                System.out.println("登录成功！");
                user.resetLoginAttempts();
                // 进入购物系统
                shoppingSystem(user);
                return;
            } else {
                attempts++;
                System.out.println("密码错误，您还有 " + (5 - attempts) + " 次尝试机会。");
            }
        }
        // 达到错误次数上限，锁定账户
        user.lockAccount();
        System.out.println("密码连续输入错误5次，账户已被锁定，请联系客服或重置密码。");
    }
    // 购物系统
    private static User shoppingSystem(User user) {
        boolean shopping = true;
        while (shopping) {
            System.out.println("==== 购物系统 ====");
            System.out.println("1. 将商品加入购物车");
            System.out.println("2. 将商品从购物车中移除");
            System.out.println("3. 修改购物车中的商品数量");
            System.out.println("4. 结账");
            System.out.println("5. 查看购物历史");
            System.out.println("6. 返回上级菜单");
            System.out.print("请选择操作：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗掉输入缓冲区的换行符

            switch (choice) {
                case 1:
                    // 将商品加入购物车
                    System.out.println("请输入要添加的商品信息：");
                    System.out.print("商品名称：");
                    String productName = scanner.next(); // 假设使用 scanner 接收用户输入
                    System.out.print("数量：");
                    int quantity = scanner.nextInt();
                    Product product = new Product(productName); // 假设 Product 类有相应的构造函数
                    ShoppingCart.addToCart(product, quantity);
                    break;
                case 2:
                    // 将商品从购物车中移除
                    ShoppingCart.removeFromCart(user);
                    break;
                case 3:
                    // 修改购物车中的商品数量
                    ShoppingCart.updateQuantity(user);
                    break;
                case 4:
                    // 结账
                    ShoppingCart.checkout(user);
                    break;
                case 5:
                    // 查看购物历史
                    ShoppingCart.displayShoppingHistory(user);
                    break;
                case 6:
                    System.out.println("返回上级菜单");
                    shopping = false;
                    break;
                default:
                    System.out.println("无效的选择，请重新选择");
                    break;
            }
        }
        return user;
    }
    // 检查密码是否符合要求的辅助方法
    private static boolean isValidPassword(String password) {
        // 密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合
        return password.length() > 8 &&
                password.matches(".*[a-z].*") &&
                password.matches(".*[A-Z].*") &&
                password.matches(".*\\d.*") &&
                password.matches(".*[!@#$%^&*()\\-_=+\\[\\]{};:\\'\",.<>/?].*");
    }
    // 内部类表示用户
    static class User {
        private String username;
        private String password;
        private boolean locked;
        private int loginAttempts;
        public User(String username, String password) {
            this.username = username;
            this.password = password;
            this.locked = false;
            this.loginAttempts = 0;
        }
        public boolean login(String passwordAttempt) {
            if (passwordAttempt.equals(password)) {
                return true;
            } else {
                loginAttempts++;
                return false;
            }
        }
        public void lockAccount() {
            locked = true;
        }
        public boolean isLocked() {
            return locked;
        }
        public void resetLoginAttempts() {
            loginAttempts = 0;
        }
        public String getName() {
            return username;
        }
        public boolean getUsername() {
            return false;
        }
    }
}
class ShoppingCart {
    private static List<CartItem> cartItems;
    private static User user;
    public ShoppingCart(User user) {
        this.user = user;
        this.cartItems = new ArrayList<>();
    }
    // 将商品加入购物车
    public static void addToCart(Product product, int quantity) {
        // 创建购物车条目
        CartItem cartItem = new CartItem(product, quantity);
        // 将购物车条目添加到购物车
        cartItems.add(cartItem);
        System.out.println(user.getUsername() + "将" + quantity + "个" + product.getName() + "加入了购物车。");
    }
    // 从购物车移除商品
    public static void removeFromCart(OnlineShoppingSystem.User product) {
        Iterator<CartItem> iterator = cartItems.iterator();
        while (iterator.hasNext()) {
            CartItem cartItem = iterator.next();
            if (cartItem.getProduct().equals(product)) {
                iterator.remove();
                System.out.println(user.getUsername() + "从购物车移除了" + product.getName());
                return;
            }
        }
        System.out.println("购物车中没有找到要移除的商品：" + product.getName());
    }
    // 修改购物车中的商品数量
    public static void updateQuantity(Product product, int newQuantity) {
        for (CartItem cartItem : cartItems) {
            if (cartItem.getProduct().equals(product)) {
                cartItem.setQuantity(newQuantity);
                System.out.println(user.getUsername() + "更新了" + product.getName() + "的数量为" + newQuantity);
                return;
            }
        }
        System.out.println("购物车中没有找到要更新数量的商品：" + product.getName());
    }

    public static void updateQuantity(OnlineShoppingSystem.User user) {
    }

    // 结账
    public static void checkout(OnlineShoppingSystem.User user) {
        // 计算总金额等结算逻辑可以在这里添加
        double total = calculateTotal();
        System.out.println(user.getUsername() + "结算了购物车，总金额为：" + total);
        // 可以在这里添加生成订单、支付等后续操作
        // 清空购物车
        clearCart();
    }
    // 清空购物车
    public static void clearCart() {
        cartItems.clear();
        System.out.println(user.getUsername() + "清空了购物车。");
    }
    // 查看购物历史
    public static void displayShoppingHistory(User user) {
        ArrayList<String> history = user.getShoppingHistory();
        System.out.println("购物历史记录 for 用户: " + user.getUsername());
        for (int i = 0; i < history.size(); i++) {
            String cart = history.get(i);
            System.out.println("购物车 #" + (i + 1) + ":");
            // 解析购物车内容
            String[] items = cart.split(";");
            for (String item : items) {
                String[] parts = item.split(",");
                String productName = parts[0];
                int quantity = Integer.parseInt(parts[1]);
                double price = Double.parseDouble(parts[2]);
                System.out.println(productName + " - 数量: " + quantity);
            }
            // 计算总金额
            double total = calculateTotalFromHistory(cart);
            System.out.println("总金额: " + total);
            System.out.println("-------------------------");
        }
    }
    // 计算历史购物车的总金额
    private static double calculateTotalFromHistory(String cart) {
        double total = 0.0;
        String[] items = cart.split(";");
        for (String item : items) {
            String[] parts = item.split(",");
            int quantity = Integer.parseInt(parts[1]);
            double price = Double.parseDouble(parts[2]);
            total += quantity * price;
        }
        return total;
    }
    private static double calculateTotal() {
        double total = 0.0;
        for (CartItem cartItem : cartItems) {
            total += cartItem.getProduct().getPrice() * cartItem.getQuantity();
        }
        return total;
    }

    public static void displayShoppingHistory(OnlineShoppingSystem.User user) {
    }

    public List<Product> getCartItems() {
        List<Product> products = new ArrayList<>();
        for (CartItem cartItem : cartItems) {
            products.add(cartItem.getProduct());
        }
        return products;
    }
    public User getUser() {
        return user;
    }
}
class CartItem {
    private Product product;
    private int quantity;
    public CartItem(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }
    public Product getProduct() {
        return product;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}